package name.flurl.android.tt_news4android;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import android.util.Log;

public class NewsEntry implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long mId = null;
	private String mTitle = null;
	private Long mDateTime = null;
	private String mShort = null;
	private String mBodyText = null;
	private String mImages = null;
	private String mCategories = null;
	private String mNewsFiles = null;
	private String mNewsSingleLink = null;

	public NewsEntry() {
		super();
	}

	public NewsEntry(Long mId, String mTitle, Long mDateTime, String mShort,
			String mBodyText, String mImages, String mCategories,
			String mNewsFiles, String mNewsSingleLink) {
		super();
		this.mId = mId;
		this.mTitle = mTitle;
		this.mDateTime = mDateTime;
		this.mShort = mShort;
		this.mBodyText = mBodyText;
		this.mImages = mImages;
		this.mCategories = mCategories;
		this.mNewsFiles = mNewsFiles;
		this.mNewsSingleLink = mNewsSingleLink;
	}

	static public NewsEntry[] fromUrl(URL url) {
		NewsEntry[] events = JsonNewsParser.fetch(url, 0);
		return events;
	}

	static public NewsEntry[] fromUrl(String url) throws MalformedURLException {
		return fromUrl(new URL(url));
	}

	static public NewsEntry fromUrlOne(URL url) {
		return fromUrl(url)[0];
	}

	static public NewsEntry fromUrlOne(String url) throws MalformedURLException {
		return fromUrl(new URL(url))[0];
	}

	private String stripTags(String s) {
		return s.replaceAll("\\<.*?\\>", "");
	}

	public String getTeaser() {
		String s = "";
		s = getmShort() + "\n" + getmBodyText();
		s = stripTags(s);
		return s.substring(0, s.length() < 50 ? s.length() : 50) + "...";
	}

	public Long getmId() {
		return mId;
	}

	public void setmId(Long mId) {
		this.mId = mId;
	}

	public String getmTitle() {
		return mTitle;
	}

	public void setmTitle(String mTitle) {
		this.mTitle = mTitle;
	}

	public Long getmDateTime() {
		return mDateTime;
	}

	public String getmDateTimeF() {
		return new SimpleDateFormat("EE dd.MM.yyyy HH:mm").format(new Date(
				mDateTime * 1000));
	}

	public void setmDateTime(Long mDateTime) {
		this.mDateTime = mDateTime;
	}

	public String getmShort() {
		return mShort;
	}

	public void setmShort(String mShort) {
		this.mShort = mShort;
	}

	public String getmBodyText() {
		return mBodyText;
	}

	public void setmBodyText(String mBodyText) {
		this.mBodyText = mBodyText;
	}

	public String getmImages() {
		return mImages;
	}

	public void setmImages(String mImages) {
		this.mImages = mImages;
	}

	public String[] getImagesArray() {
		String[] images = null;
		String imageStr = this.getmImages();
		Log.i("tt_news splitting string", imageStr + "#");
		if (!imageStr.equals("")) {
			images = imageStr.trim().split(",");
		}
		return images;
	}

	public String getmCategories() {
		return mCategories;
	}

	public void setmCategories(String mCategories) {
		this.mCategories = mCategories;
	}

	public String[] getCategories() {
		return mCategories.split(",");
	}

	public boolean hasCategory(String cat) {
		return Arrays.asList(getCategories()).contains(cat);
	}

	public String getmNewsFiles() {
		return mNewsFiles;
	}

	public void setmNewsFiles(String mNewsFiles) {
		this.mNewsFiles = mNewsFiles;
	}

	public String[] getNewsFilesArray() {
		String[] files = {};
		String filesStr = getmNewsFiles();
		if (!filesStr.trim().equals("")) {
			files = filesStr.split(",");
		}
		return files;
	}

	public String getmNewsSingleLink() {
		return mNewsSingleLink;
	}

	public void setmNewsSingleLink(String mNewsSingleLink) {
		this.mNewsSingleLink = mNewsSingleLink;
	}
}
